<?php
/**
 *台湾代表处转为劳保局
*/
namespace app\admin\controller;
use app\admin\controller\Admin;
use think\facade\Db;
use xhadmin\service\admin\record\CustomerService;
use xhadmin\service\admin\MemberService;
use xhadmin\db\Customer as CustomerDb;
use think\facade\Cache;

class Stepfour extends Admin {


	/*会员管理*/
	function index(){
        if (!$this->request->isAjax()){
			return $this->display('index');
		}else{
            $limit  = $this->request->post('limit', 0, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$id_card= $this->request->param('id_card', '', 'strip_tags,trim');
			$first_name = $this->request->param('first_name', '', 'strip_tags,trim');
			$birth_year = $this->request->param('birth_year', '', 'strip_tags,trim');
			$last_name = $this->request->param('last_name', '', 'strip_tags,trim');
            $id= $this->request->param('id', '', 'strip_tags,trim');

            if(!empty($id)){
                $where['id'] = ['=',$id];
            }
            if(!empty($id_card)){
                $where['id_card'] = ['like','%'.$id_card.'%'];
            }
            if(!empty($birth_year)){
                $where['birth_day'] = ['like','%'.$birth_year.'%'];
            }
            if(!empty($first_name)){
                $where['first_name|last_name|passport_info'] = ['like','%'.$first_name.'%'];
            }
            if(!empty($last_name)){
                $where['last_name'] = ['like','%'.$last_name.'%'];
            }
            $where['status'] = ['=',4];
			$limit = ($page-1) * $limit.','.$limit;
            //p($where);exit;
			try{
				$res = CustomerService::pageListA(formatWhere($where),$limit,'*',$orderby = 'id desc',4);
				foreach ($res['list'] as $key=>&$val) {
				    $val['work_year_month'] = $val['work_year'].'年'.$val['work_month'].'个月';
                    $val['name'] = $val['first_name'].' '.$val['last_name'];
                }
				$list = $res['list'];
			}catch(\Exception $e){
				exit($e->getMessage());
			}

			$data['rows']  = $list;
			$data['total'] = $res['count'];
			return json($data);
		}
	}



	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','intval');
			if(!$id) $this->error('参数错误');
			$this->view->assign('info',checkData(CustomerDb::getInfo($id)));
			return $this->display('steptwo/info');
		}else{
            $postField = 'id,status,id_card,idcardimg,idcardimgel,bankimg,wjbhzimg,first_name,last_name,birth_day,
			religion,addr,expiry_date,id_card_upside_no,work_year,work_month,mobile,director,is_rename,is_hz_dg,
			is_yhb_dg,is_introducer,introducer';
			$data = $this->request->only(explode(',',$postField),'post');
			try {
                CustomerService::update($data);
			} catch (\Exception $e) {
				$this->error($e->getMessage());
			}
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

    /*改为转为劳保局送件状态*/
    function status() {
        if ($this->request->isPost()){
            $postField = 'ids';
            $data = $this->request->only(explode(',',$postField),'post');
            try {
                $now = time();
                if(strstr($data['ids'],',')) {
                    $ids = explode(',',$data['ids']);
                    CustomerService::editWhere([['id','in',$ids]],['status'=>5]);
                    foreach ($ids as $key=>$val) {
                        $info = CustomerDb::getInfo($val);
                        $customer_log[] = [
                            'c_id' => $val,
                            'id_card' => $info['id_card'],
                            'status' => 5,
                            'create_by' => session('admin.userid'),
                            'create_time' => $now,
                        ];
                    }
                    Db::name("customer_log")->insertAll($customer_log);
                } else {
                    $id = $data['ids'];
                    $info = CustomerDb::getInfo($id);
                    $customer_log = [
                        'c_id' => $id,
                        'id_card' => $info['id_card'],
                        'status' => 5,
                        'create_by' => session('admin.userid'),
                        'create_time' => $now,
                    ];
                    Db::name("customer_log")->insert($customer_log);
                    CustomerService::update(['id'=>$id,'status'=>5]);
                }
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            return json(['status'=>'00','msg'=>'修改成功']);
        }
    }

	/*删除*/
	function delete(){
		$idx =  $this->request->post('ids', '', 'strip_tags');
		if(!$idx) $this->error('参数错误');
		try{
			$where['id'] = explode(',',$idx);
            CustomerService::delete($where);
		}catch(\Exception $e){
			$this->error($e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看数据*/
	function view(){
		$id = $this->request->get('id','','intval');
		if(!$id) $this->error('参数错误');
		try{
			$this->view->assign('info',checkData(CustomerDb::getInfo($id)));
			return $this->display('record/customer/view');
		} catch (\Exception $e){
			$this->error($e->getMessage());
		}
	}



    function getThumbPic(){
        $detail = $this->request->post('detail','','strval');
        preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/',$detail,$allImg);
        return json(['status'=>'00','imgurl'=>$allImg[1][0]]);
    }


    function export()
    {
        $id_card= $this->request->param('id_card', '', 'strip_tags,trim');
        $first_name = $this->request->param('first_name', '', 'strip_tags,trim');
        $birth_year = $this->request->param('birth_year', '', 'strip_tags,trim');
        $last_name = $this->request->param('last_name', '', 'strip_tags,trim');
        $id= $this->request->param('id', '', 'strip_tags,trim');

        if(!empty($id)){
            $where['id'] = ['=',$id];
        }
        if(!empty($id_card)){
            $where['id_card'] = ['like','%'.$id_card.'%'];
        }
        if(!empty($birth_year)){
            $where['birth_day'] = ['like','%'.$birth_year.'%'];
        }
        if(!empty($first_name)){
            $where['first_name|last_name|passport_info'] = ['like','%'.$first_name.'%'];
        }
        if(!empty($last_name)){
            $where['last_name'] = ['like','%'.$last_name.'%'];
        }
        $where['status'] = ['=',4];
        $limit = 500;

        try{
            //$res = CustomerService::pageList($where,$limit,$field='*',$orderby='');
            $res = CustomerService::pageListA(formatWhere($where),$limit,'*',$orderby = 'id desc',4);
            //p($res);exit;
//            foreach ($res['list'] as $key=>&$val) {
//                $val['cat_id'] = self::$materia_category[$val['cat_id']];
//                $val['status'] = self::$materia_status[$val['status']];
//                $val['quotation_unit'] = self::$quotation_unit[$val['quotation_unit']];
//            }

            $list = $res['list'];
        }catch(\Exception $e){
            exit($e->getMessage());
        }
        //p($list);exit;
        if(!empty($list)){
            $ids = array_column($list,'id');
            $where_log[] = ['c_id','in',$ids];
            $customer_log = Db::name('customer_log')->where($where_log)->select()->toArray();
            $customer_log = array_column($customer_log,null,'c_id');

            $translater_ids = array_column($list,'translater_id');
            $translater_log[] = ['id','in',$translater_ids];
            $translater_list = Db::name('translator')->where($translater_log)->select()->toArray();
            $translater_list = array_column($translater_list,null,'id');
            //p($translater_list);exit(sas);
        }

        //p($list);exit;
        $list_new = [];
        $i = 1;
        $gy = 0;
        foreach ($list as $val) {
            $list_new[] = [
                'no' => $i,
                'name' => $val['first_name'].$val['last_name'],
                'rznr' => $val['ggz']>3?'身份证+护照':'身份证',
                'ggz' => $val['ggz'],
                'gy' => $val['ggz']*510,
                'sjrq' => isset($customer_log[$val['id']]['create_time'])?date('Y-m-d',$customer_log[$val['id']]['create_time']):'',
                'ljrq' => date('Y-m-d',time()),
                'strq' => date('Y-m-d',time()+3600*24),
                'id' =>$val['id'],
                'translater'=>isset($translater_list[$val['translater_id']]['name'])?$translater_list[$val['translater_id']]['name']:''
            ];
            $gy+= $gy;
            $i++;
        }
        $last = [
            'no' => '',
            'name' => '',
            'rznr' => '',
            'ggz' => '总计',
            'gy' => $gy,
            'sjrq' => '',
            'ljrq' => '',
            'strq' => '',
            'id' =>'',
            'translater'=>''
        ];
        array_push($list_new,$last);
        //p($list_new);exit;
        $headers = [['列号'], ['姓名'], ['认证内容'], ['共盖章'],['共银'], ['送件日期'], ['领件日期'],
            ['送台日期'],['编号'], ['翻译人']];
        $list_new = restore_array($list_new);
        $file = \xhadmin\db\Common::dumpData($headers, $list_new);
        //exit('sasa');
        if ($file) {
            return json(['status' => '00', 'msg' => 'ok', 'data' => ['file_name' => $file]]);
        }
    }



}

